package sctu.edu.java.year2016.team06.liuchuanjiao.Practise;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * Created by LCJ on 2016/6/15.
 */
public class Prac extends JFrame {

    /*
    public void CreateJFrame(String title){
        JFrame jf=new JFrame(title);
        Container container=jf.getContentPane();
        JLabel jl=new JLabel("hello");
        jl.setHorizontalAlignment(SwingConstants.CENTER);
        container.add(jl);

        container.setBackground(Color.green);
        JButton BL=new JButton("弹出对话框");
        JPanel contentPane = new JPanel();

        setContentPane( contentPane);
        container.add(BL);
        jf.setVisible(true);
        jf.setSize(200, 100);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Prac().CreateJFrame("YES");
    }*/
    /*
    public Prac() {
        setTitle("窗体");
        Container c = getContentPane();
        //setLayout(new FlowLayout(2, 10, 10));
        //for (int i = 0; i < 5; i++) {
         //   c.add(new JButton("HELLO" + i));
        //}
        JTextArea ta=new JTextArea(20,50);
        ta.setText("wwwwwww");
        JScrollPane SP=new JScrollPane(ta);
        c.add(SP);
        setSize(300, 200);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Prac();
    }*/
    //public Prac() {
       //setTitle("窗体");

        //Container c = getContentPane();
        //Container panel1 = getContentPane();
       // Container panel2 = getContentPane();
       //c.setLayout(new BorderLayout());
       // c.add(panel1, BorderLayout.NORTH);
        //c.add(panel2, BorderLayout.SOUTH);
       // final JScrollPane jscrollPane = new JScrollPane();
        //panel1.add(jscrollPane);
        //JRadioButton jc1 = new JRadioButton();
       // JRadioButton jc2 = new JRadioButton();
        //JRadioButton jc3 = new JRadioButton();
        //ButtonGroup group = new ButtonGroup();
        //group.add(jc1);
        //group.add(jc2);
        //group.add(jc3);


       // setSize(300, 200);
       // setVisible(true);
       // setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   // }
        //public static void main(String[] args) {
         //   new Prac();
       // }
    /*
    public Prac() {
        Container container = getContentPane();
        container.setLayout(null);
        JLabel JL = new JLabel("KKK");
        JL.setHorizontalAlignment(SwingConstants.CENTER);
        container.add(JL);
        JButton BL = new JButton("弹出");
        BL.setBounds(10, 10, 100, 21);
        BL.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Prac2().setVisible(true);
            }
        });
        container.add(BL);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Prac2();
    }*/

    private  Thread t;
    private static  Color[] color={Color.BLACK,Color.GREEN,Color.BLUE,Color.RED};
    private  static  final Random rand=new Random();
    private static Color getC(){
        return  color[rand.nextInt(color.length)];
    }
    public Prac(){
        t=new Thread(new Runnable() {
            int x=300;
            int y=500;
            @Override
            public void run() {
                while (true){
                    try {
                        t.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Graphics graphics=getGraphics();
                    graphics.setColor(getC());
                    graphics.drawLine(x,y,100,y++);
                    if(y>=800){
                        y=500;
                    }
                }

            }
        });
        t.start();
    }

    public static void main(String[] args) {
        init(new Prac(), 1000, 1000);
    }
    public static void init(JFrame jframe,int width,int heigth){
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setSize(width,heigth);
        jframe.setVisible(true);
    }

}




